$('._profile').livequery(function(){
	$(".orders-list .order").click( function(event) {
		clickedElement=event.target||event.srcElement; 
		if (clickedElement.tagName != 'A') {
			url = W_ABSPATH + 'profile/orders/' + $(this).attr('id');
			location.href = url;
		}
	});
});

$('.edit-profile').livequery(function() {
	$( ".city-field" ).autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: W_AJAX + 'city/get',
						dataType: "jsonp",
						type : 'POST',
						data: {
							nameStartWith: request.term
						},
						success: function( data ) {
							response( $.map( data.citiesNames, function( item ) {
								return {
									label: item.city_name,
									value: item.city_name,
								}
							}));
						}
			});
		},
		minLength: 2
	});
});

$('.money-stat').livequery(function(){
	$('.orders-money .month-stat').on('mouseenter mouseleave click mousedown', function(e) {
		if(!$(this).hasClass('active')) {
			if(e.type == 'click') showItem($(this));
			if(e.type == 'mouseenter') $(this).addClass('hover');
		}
		else if(e.type == 'click')  {
			clickedElement=event.target||event.srcElement; 
			if (clickedElement.tagName != 'A') hideItem($(this));
			else return true;
		}
		if(e.type == 'mouseleave') $(this).removeClass('hover');
		return false;
	});
	
	$('.year-res').each(function() {
		var self = $(this);
		var yearSummWait = 0;
		var yearSummFact = 0;
		$('.order .money span', self).each(function(){
			if($(this).closest('.order').hasClass('wait')) yearSummWait += parseInt($(this).text());
			else yearSummFact += parseInt($(this).text());
			
		});
		
		$('.year-summ-fact', self).append('<span>' + ($('.orders-money').hasClass('cust') ? 'Текущий' : 'Текущая') + ': </span>' + yearSummFact + ' руб.');
		if(yearSummWait != 0) $('.year-summ-wait', self).append('<span>' + ($('.orders-money').hasClass('cust') ? 'Ожидаемый' : 'Ожидаемая') + ': </span>' + (yearSummFact + yearSummWait) + ' руб.');
	});
	
	$('.month-stat').each(function() {
		var self = $(this);
		var monthSummWait = 0;
		var monthSummFact = 0;
		$('.order .money span', self).each(function(){
			if($(this).closest('.order').hasClass('wait')) monthSummWait += parseInt($(this).text());
			else monthSummFact += parseInt($(this).text());
		});

		$('.month-summ-fact', self).append('<span>' + ($('.orders-money').hasClass('cust') ? 'Текущий' : 'Текущая') + ': </span>' + monthSummFact + ' руб.');
		if(monthSummWait != 0) $('.month-summ-wait', self).append('<span>' + ($('.orders-money').hasClass('cust') ? 'Ожидаемый' : 'Ожидаемая') + ': </span>' + (monthSummFact + monthSummWait) + ' руб.');
	});
	
	$(".orders-list .order").click( function(event) {
		clickedElement=event.target||event.srcElement; 
		if (clickedElement.tagName != 'A') {
			url = W_ABSPATH + 'profile/orders/' + $(this).attr('id');
			location.href = url;
		}
	});
	
});

function showItem(item) {
	var addInf = item.children('.orders');
	addInf.slideDown(100, function(){$('.orders').clearQueue(); item.addClass('active')});
}

function hideItem(item) {
	var addInf = item.children('.orders');
	addInf.slideUp(100, function(){$('.orders').clearQueue(); item.removeClass('active')});
}